home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS20.ADF / BobEd / events.c < prev    next >
C/C++ Source or Header  |  1989-01-27  |  17KB  |  461 lines

  1. #include <bobed.h>
  2.  
  3. /*
  4.    "events.c" - BobEd event processor
  5.    These routines are the guts of the editor.
  6. */
  7.  
  8. VOID  drawx ();         /* draw a crosshair for saving object           */
  9. VOID  drawrbox ();      /* draw a rubber box for framing object         */
  10. int   frame ();         /* frame object for saving                      */
  11. VOID  copyview ();      /* copy one object to another frame             */
  12. VOID  rewrite ();       /* redraw the object                            */
  13. VOID  clear ();         /* clear the current object                     */
  14. VOID  flip ();          /* flip the current object on its y axis        */
  15. VOID  rotate ();        /* rotate current object 90 degrees right       */
  16. VOID  makecolor ();     /* make selected color current                  */
  17. VOID  newview ();       /* make selected object current                 */
  18. VOID  drawpixel ();     /* draw big pixel and small one in current ob   */
  19. int   clip ();          /* determine if mouse is outside edit box       */
  20.  
  21. extern int save ();  /* defined in disk.c */
  22. extern int load ();
  23.  
  24. extern struct Window   *w;
  25. extern struct Screen   *screen;
  26. extern struct RastPort *rp;
  27. extern SHORT  colorbox [];
  28. extern SHORT  boxes [2][HBOBS*VBOBS];
  29.  
  30. extern struct Requester  loadreq;
  31. extern struct Requester  savereq;
  32. extern struct Requester  inforeq;
  33. extern struct Gadget     formatgad; /* used to check desired save format */
  34. extern struct StringInfo stringinfo;/* file name */
  35. extern struct StringInfo intinfo;   /* number of bit planes to save */
  36.  
  37. extern UBYTE  bobdata [OBS+1][BWIDE*16][BHIGH]; /* image data */
  38.  
  39. UBYTE touched [OBS]; /* has this image been modified? */
  40.  
  41. VOID doevent ()
  42. {
  43.    USHORT   class;               /* IDCMP message class  */
  44.    USHORT   code;                /* IDCMP message code   */
  45.    APTR     address;             /* IDCMP message address */
  46.    struct   IntuiMessage   *m1;  /* IDCMP message        */
  47.    USHORT   msx,msy;             /* mouse coordinates    */
  48.    BOOL     pendown=FALSE;       /* pen status           */
  49.    UBYTE    color=1;             /* current drawing color*/
  50.    UBYTE    id,reqid;            /* gadget id code       */
  51.    UBYTE    view=0;              /* current image        */
  52.    UBYTE    menunum,itemnum;     /* menu number, item num*/
  53.    USHORT   x0,y0,x1,y1;         /* frame size           */
  54.    UBYTE    savestat;            /* load/save success    */
  55.    UBYTE    planes=2;            /* bit planes to save   */
  56.    UBYTE    ccode=TRUE;          /* flag for c format    */
  57.  
  58.    makecolor (color);   /* fill color selected box with current color   */
  59.    FOREVER              /* repeat until user quits */
  60.    {
  61.       Wait (1<<w->UserPort->mp_SigBit);   /* wait for a message   */
  62.       {
  63.  
  64.    /*
  65.       this is not the most efficient way to get the messages, fast
  66.       messages may go unnoticed (MOUSEMOVE in specific).
  67.       proper way is to get messages until the queue is empty
  68.       and then process the events.  V1.0 falls behind if the mouse
  69.       is moved quickly.
  70.     */
  71.  
  72.          while ((m1=(struct IntuiMessage *)GetMsg(w->UserPort)))
  73.          {
  74.             class = m1->Class;   /* get the info we need    */
  75.             code  = m1->Code;
  76.             address = m1->IAddress;
  77.             msx = m1->MouseX;
  78.             msy = m1->MouseY;
  79.             ReplyMsg (m1);       /* and reply right away !  */
  80.             switch (class)
  81.             {
  82.                case MOUSEMOVE:
  83.                   if (!pendown) break;
  84.                   if (!clip (msx,msy)) /* if inside draw box then draw */
  85.                      drawpixel (msx,msy,color,view);
  86.                   break;   /* if ouside draw box then dont do anything */
  87.                case MOUSEBUTTONS:
  88.                   if (code == SELECTDOWN) /* left button pressed       */
  89.                   {
  90.                      pendown = TRUE;   /* put the pen down to draw */
  91.                      if (!clip (msx,msy)) drawpixel (msx,msy,color,view);
  92.                   }
  93.                   else pendown = FALSE; /* must be SELECTUP so dont draw */
  94.                   break;
  95.                case GADGETUP: /* one of many possible gadgets selected */
  96.                {
  97.                   id = ((struct Gadget *)address)->GadgetID; /* get ID */
  98.                   if (id < 16)   /* first 16 are the color boxes */
  99.                   {
  100.                      color = id; /* easy color select */
  101.                      makecolor (color); /* change the color selected box */
  102.                      break;
  103.                   } else if (id < REQBASE) { /* is it a view select gad? */
  104.                      view = id-VIEWBASE;  /* sub offset to get view sel  */
  105.                      if (touched[view] == 0) /* has this view been modif? */
  106.                         clear (view); /* if not, just clear draw box */
  107.                      else   /* if it has then redraw draw box */
  108.                         newview (view);
  109.                      break;
  110.                   } else { /* a requester gadget has been hit */
  111.                      reqid = id-REQBASE; /* get the ID   */
  112.                      switch (reqid)
  113.                      {
  114.                         case 0: /* load requester gadget selected */
  115.                            if (stringinfo.NumChars > 0) /* no file name? */
  116.                            {
  117.                               copyview (view,9); /* save in case of unsucessful load */
  118.                               clear (view); /* clear array & view */
  119.                               savestat = load (view,stringinfo.Buffer);
  120.                               if (savestat == FALSE) copyview (9,view); /*restore old view */
  121.                               else touched[view] = TRUE;
  122.                               rewrite (view); /* draw into small view */
  123.                               newview (view); /* draw into draw box   */
  124.                            }
  125.                            break;
  126.                         case 3: /* save requester gadget selected */
  127.                            if (stringinfo.NumChars > 0) /* no file name?*/
  128.                            {
  129.                               if ((formatgad.Flags & SELECTED) > 0) /* save as C code? */
  130.                               {
  131.                                  ccode = TRUE;
  132.                               } else
  133.                                  ccode = FALSE;
  134.                               savestat = frame (view,&x0,&y0,&x1,&y1); /*draw frame around desired image */
  135.                               planes = (SHORT)intinfo.LongInt; /* number ofplanes */
  136.                               if (savestat == TRUE) /* user did not cancel*/
  137.                                  savestat = save (ccode,stringinfo.Buffer,view,x0,y0,x1,y1,planes);
  138.                               SetDrMd (rp, JAM1);
  139.                               pendown=FALSE;
  140.                            }
  141.                            break;
  142.                         default: /* cancel */
  143.                            break;
  144.                      }
  145.                      break;
  146.                   }
  147.                }
  148.                case MENUPICK: /* user pressed right button */
  149.                {
  150.                   if (code != MENUNULL) /* if button not pressed on the menu
  151.                                           bar, code will=MENUNULL */
  152.                   {
  153.                      menunum = MENUNUM (code); /* which menu */
  154.                      itemnum = ITEMNUM (code); /* which item */
  155.                      switch (menunum)
  156.                      {
  157.                         case 0:  /* file menu selected */
  158.                            switch (itemnum)
  159.                            {
  160.                               case 0: /* user wants info */
  161.                                  Request (&inforeq,w);
  162.                                  break;
  163.                               case 1: /* user wants to load */
  164.                                  Request (&loadreq,w);
  165.                                  break;
  166.                               case 2: /* user wants to save */
  167.                                  Request (&savereq,w);
  168.                                  break;
  169.                               case 3: /* user wants to quit */
  170.                                  bye (NULL);
  171.                               default:
  172.                                  break;
  173.                            }
  174.                            break;
  175.                         case 1:
  176.                            switch (itemnum)
  177.                            {
  178.                               case 0: /* clear the present view */
  179.                                  clear (view);
  180.                                  break;
  181.                               case 1: /* flip the present view */
  182.                                  flip (view);
  183.                                  rewrite (view);
  184.                                  newview (view);
  185.                                  break;
  186.                               case 2: /* rotate the present view */
  187.                                  rotate (view);
  188.                                  rewrite (view);
  189.                                  newview (view);
  190.                                  break;
  191.                               default: /* ignore anything else */
  192.                                  break;
  193.                            }
  194.                            break;
  195.                         case 2:
  196.                            switch (itemnum)
  197.                            {
  198.                               case 0: /* take a snapshot of this view */
  199.                                  copyview (view,9);
  200.                                  break;
  201.                               case 1: /* copy the snapshot */
  202.                                  touched[view]=TRUE;
  203.                                  copyview (9,view);
  204.                                  rewrite (view);
  205.                                  newview (view);
  206.                                  break;
  207.                            }
  208.                         default:
  209.                            break;
  210.                      }
  211.                   }
  212.                }
  213.             }
  214.          }
  215.       }
  216.    }
  217. }
  218.  
  219. VOID  rotate (v)  /* rotate the view 90 deg. to the right */
  220. SHORT v;
  221. {
  222.    REGISTER int   x,y;
  223.  
  224.    for (y=0;y<BWIDE*16;y++)
  225.       for (x=0;x<BWIDE*16;x++)
  226.          bobdata [9][BWIDE*16-y-1][x] = bobdata [v][x][y];
  227.    copyview (9,v);
  228. }
  229.  
  230. VOID  copyview (s,t)
  231. SHORT s,t;
  232. {
  233.    REGISTER int   x,y;
  234.  
  235.    for (y=0;y<BHIGH;y++)
  236.       for (x=0;x<BWIDE*16;x++)
  237.          bobdata [t][x][y] = bobdata [s][x][y];
  238. }
  239.  
  240. VOID  flip (v) /* flip this view */
  241. SHORT v;
  242. {
  243.    REGISTER int   x,y,c;
  244.  
  245.    for (y=0;y<BHIGH;y++)
  246.       for (x=0;x<(BWIDE*8);x++)
  247.       {
  248.          c = bobdata [v][x][y];
  249.          bobdata [v][x][y] = bobdata [v][(BWIDE*16)-x-1][y];
  250.          bobdata [v][(BWIDE*16)-x-1][y] = c;
  251.       }
  252. }
  253.  
  254. VOID  rewrite (v) /* redraw the small view */
  255. SHORT v;
  256. {
  257.    REGISTER int   x,y;
  258.    int            c;
  259.  
  260.    for (y=0;y<BHIGH;y++)
  261.       for (x=0;x<BWIDE*16;x++)
  262.       {
  263.          SetAPen (rp, bobdata [v][x][y]); /* the data contains the color */
  264.          c=WritePixel (rp, boxes [0][v]+x,boxes [1][v]+y);
  265.       }
  266. }
  267.  
  268. VOID  clear (v) /* clear the data and blank both views */
  269. SHORT v;
  270. {
  271.    REGISTER int   x,y;
  272.  
  273.    touched[v]=0;
  274.    for (y=0;y<BHIGH;y++)
  275.       for (x=0;x<(BWIDE*16);x++)
  276.          bobdata [v][x][y] = 0;
  277.    SetAPen (rp,0);   /* fill both boxes with background colors */
  278.    RectFill (rp, LOFFSET,TOFFSET,HWIDE-ROFFSET-1,HIGH-BOFFSET-1);
  279.    RectFill (rp,boxes[0][v],boxes[1][v],boxes[0][v]+BWIDE*16-1,boxes[1][v]+BHIGH-1);
  280. }
  281.  
  282. int   clip (x,y)  /* check to see if user wants to draw outside the bigbox*/
  283. int   x,y;
  284. {
  285.    if ((x > LOFFSET+1) && (x < (HWIDE-ROFFSET-1)) && (y < (HIGH-BOFFSET-1)) && (y> TOFFSET+1))
  286.       return (FALSE);   /* it is ok */
  287.    return (TRUE);       /* it is outside boundary */
  288. }
  289.  
  290. VOID  drawpixel (x,y,c,v) /* draw one dot in the little view and the bigbox*/
  291. int   x,y,c,v;
  292. {
  293.    int   xx,yy;
  294.    int   xind,yind;
  295.  
  296.    xind = (x-LOFFSET-1)/HBLOCK;
  297.    yind = (y-TOFFSET-1)/VBLOCK;
  298.    xx   = LOFFSET+1+xind*HBLOCK;
  299.    yy   = TOFFSET+1+yind*VBLOCK;
  300.  
  301.    touched[v]=1; 
  302.    SetAPen (rp, c);
  303.    RectFill (rp, xx,yy,(xx+HBLOCK-1),(yy+VBLOCK-1));
  304.    xx=WritePixel (rp, boxes [0][v]+xind,boxes [1][v]+yind);
  305.    bobdata [v][xind][yind] = (UBYTE)c;
  306. }
  307.  
  308. VOID  makecolor (c) /* fill the color selected box with the current color */
  309. SHORT c;
  310. {
  311.    SetAPen (rp, c);
  312.    RectFill (rp, colorbox [0]+1,colorbox [1]+1,colorbox [4]-1,colorbox [5]-1);
  313. }
  314.  
  315. VOID  newview (v) /* redraw the data into the big draw box */
  316. SHORT v;
  317. {
  318.    REGISTER int   xx,yy;
  319.    int      x,y,lo,to,bw;
  320.  
  321.    lo = LOFFSET+1;
  322.    to = TOFFSET+1;
  323.    bw = BWIDE*16;
  324.  
  325.    for (yy=0;yy<BHIGH;yy++)
  326.    {
  327.       y   = to+yy*VBLOCK;
  328.       for (xx=0;xx<bw;xx++)
  329.       {
  330.          x   = lo+xx*HBLOCK;
  331.          SetAPen (rp, bobdata [v][xx][yy]);
  332.          RectFill (rp, x,y,x+HBLOCK-1,y+VBLOCK-1); /* draw a fat pixel */
  333.       }
  334.    }
  335. }
  336.  
  337. int   frame (v,x0,y0,x1,y1)   /* frame the portion of the view
  338.                                  you want to save              */
  339. SHORT v,*x0,*y0,*x1,*y1;
  340. {
  341.    USHORT   class;            /* we get our own events in this routine */
  342.    USHORT   code;             /* see doevent for explanation of these  */
  343.    struct   IntuiMessage   *m1;
  344.    int      msx,msy,t;
  345.    int      oldx=0,oldy=0;    /* save previous mouse position  */
  346.    int      rubberbox=FALSE;
  347.  
  348.    FOREVER
  349.    {
  350.       Wait (1<<w->UserPort->mp_SigBit);
  351.       {
  352.          while ((m1=(struct IntuiMessage *)GetMsg(w->UserPort)))
  353.          {
  354.             class = m1->Class;
  355.             code  = m1->Code;
  356.             msx = m1->MouseX;
  357.             msy = m1->MouseY;
  358.             ReplyMsg (m1);
  359.             switch (class)
  360.             {
  361.                case MOUSEMOVE:
  362.                   if (clip (msx,msy))
  363.                   {
  364.                      if (msx < LOFFSET+1) msx = LOFFSET+1;
  365.                      if (msy < TOFFSET+1) msy = TOFFSET+1;
  366.                      if (msx > HWIDE-ROFFSET-1) msx = HWIDE-ROFFSET-1;
  367.                      if (msy > HIGH-BOFFSET-1) msy = HIGH-BOFFSET-1;
  368.                   }
  369.                   if (rubberbox == TRUE)
  370.                   {
  371.                      drawrbox (*x0,*y0,*x1,*y1);
  372.                      msx=(msx-LOFFSET-1)/HBLOCK;
  373.                      msy=(msy-TOFFSET-1)/VBLOCK;
  374.                      *x1=msx;*y1=msy;
  375.                      drawrbox (*x0,*y0,*x1,*y1);
  376.                      break;
  377.                   } else {
  378.                      if (oldx != 0)
  379.                         drawx (oldx,oldy);
  380.                      oldx = msx;
  381.                      oldy = msy;
  382.                      drawx (msx,msy);
  383.                   }
  384.                   break;
  385.                case MOUSEBUTTONS:
  386.                   switch (code)
  387.                   {
  388.                      case SELECTUP:
  389.                         if (rubberbox == TRUE)
  390.                         {
  391.                            drawrbox (*x0,*y0,*x1,*y1);
  392.                            if ((*x0 == *x1) || (*y0 == *y1)) return (FALSE);
  393.                            if (*x0 > *x1) { t=*x1; *x1=*x0; *x0=t; }
  394.                            if (*y0 > *y1) { t=*y1; *y1=*y0; *y0=t; }
  395.                            return (TRUE);
  396.                         }
  397.                         else
  398.                         {
  399.                            if (rubberbox == FALSE)
  400.                            {
  401.                               if (clip (msx,msy))
  402.                               {
  403.                                  if (msx < LOFFSET+1) msx = LOFFSET+1;
  404.                                  if (msy < TOFFSET+1) msy = TOFFSET+1;
  405.                                  if (msx > HWIDE-ROFFSET-1) msx = HWIDE-ROFFSET-1;
  406.                                  if (msy > HIGH-BOFFSET-1) msy = HIGH-BOFFSET-1;
  407.                               }
  408.                               drawx (oldx,oldy);
  409.                               rubberbox=TRUE;
  410.                               msx=(msx-LOFFSET-1)/HBLOCK;
  411.                               msy=(msy-TOFFSET-1)/VBLOCK;
  412.                               *x0=msx; *x1=msx;
  413.                               *y0=msy; *y1=msy;
  414.                               drawrbox (*x0,*y0,*x1,*y1);
  415.                               break;
  416.                            }
  417.                         }
  418.                   }
  419.                default:
  420.                   break;
  421.             }
  422.          }
  423.       }
  424.    }
  425. }
  426.  
  427. VOID  drawrbox (x0,y0,x1,y1)
  428. SHORT x0,y0,x1,y1;
  429. {
  430.    x0   = LOFFSET+1+x0*HBLOCK-1;
  431.    y0   = TOFFSET+1+y0*VBLOCK-1;
  432.    x1   = LOFFSET+1+x1*HBLOCK;
  433.    y1   = TOFFSET+1+y1*VBLOCK;
  434.  
  435.    SetAPen (rp,1);
  436.    SetDrMd (rp, COMPLEMENT);
  437.    Move (rp, x0,y0);
  438.    Draw (rp, x1,y0);
  439.    Draw (rp, x1,y1);
  440.    Draw (rp, x0,y1);
  441.    Draw (rp, x0,y0);
  442. }
  443.  
  444. VOID  drawx (x,y)
  445. SHORT x,y;
  446. {
  447.    SetAPen (rp,1);
  448.  
  449.    x = (x-LOFFSET-1)/HBLOCK;
  450.    x = LOFFSET+1+x*HBLOCK-1;
  451.    y = (y-TOFFSET-1)/VBLOCK;
  452.    y = TOFFSET+1+y*VBLOCK-1;
  453.  
  454.    SetDrMd (rp, COMPLEMENT);
  455.    Move (rp, x,TOFFSET+1);
  456.    Draw (rp, x,HIGH-BOFFSET-1);
  457.    Move (rp, LOFFSET+1,y);
  458.    Draw (rp, HWIDE-ROFFSET-1,y);
  459. }
  460.  
  461.